package com.suineg.pms.web.payfor;


import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.suineg.pms.entity.base.Room;
import com.suineg.pms.entity.fee.FeePlan;
import com.suineg.pms.entity.fee.FeeScal;
import com.suineg.pms.service.base.RoomService;
import com.suineg.pms.service.fee.FeePlanService;
import com.suineg.pms.service.fee.FeeScaleService;

/**
 * 收费
 *@author Suine
 */
@Controller
@RequestMapping(value = "/payfor")
public class PayController {
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	/** 收费方案服务层 对象 */
	private FeePlanService feePlanService;
	/** 收费项目服务层对象 */
	private FeeScaleService feeScaleService;
	/** 房间管理  服务层对象 */
	private RoomService roomService;
	
	@RequestMapping(value = "pay")
	public String list(){
		return "payfor/pay";
	}
	
	/**
	 * 获取所有公用方案
	 * @return
	 */
	@RequestMapping(value = "loadPlan")
	@ResponseBody
	public String loadPlan(){
		JSONArray array = new JSONArray();
		JSONObject obj = null;
		List<FeePlan> list = feePlanService.findByPublic();
		if(list == null || list.isEmpty()){
			return "false";
		}
		try {
			for(FeePlan f : list){
				obj = new JSONObject();
				obj.put("id", f.getId());
				obj.put("name", f.getName());
				array.put(obj);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return array.toString();
	}
	
	/**
	 * 查看信息
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "input/{id}" , method = RequestMethod.GET)
	public String input(@PathVariable("id") Long id,Model model){
		Room room = roomService.get(id);
		List<FeePlan> personalFeePlan = room.getPlans();
		List<FeePlan> publicFeePlan = feePlanService.findByPublic();
		List<FeeScal> allScal = feeScaleService.findAll();
		
		model.addAttribute("personalPaln", personalFeePlan);
		model.addAttribute("publicFeePlan", publicFeePlan);
		model.addAttribute("allScal", allScal);
		model.addAttribute("room", room);
		model.addAttribute("id", id);
		return "payfor/payInput";
	}
	
	/**
	 * 所有收费项目.
	 * @return
	 */
	@RequestMapping(value = "loadAllScale")
	@ResponseBody
	public String loadAllScale(){
		JSONArray array = new JSONArray();
		JSONObject obj = null;
		List<FeeScal> scals = feeScaleService.findAll();
		if(scals == null || scals.isEmpty()){
			return "false";
		}
		try {
			for(FeeScal s : scals){
				obj = new JSONObject();
				obj.put("id", s.getId());
				obj.put("name", s.getName());
				array.put(obj);
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return array.toString();
	}
	
	/**
	 * 保存
	 * @return
	 */
	@RequestMapping(value = "save",method = RequestMethod.POST)
	public String save(@RequestParam(value = "data") List<Map<String,String>> data ,RedirectAttributes redirectAttributes ){
		logger.info(data.toString());
		return "payfor/pay";
	}
	
	
	@Autowired
	public void setFeePlanService(FeePlanService feePlanService) {
		this.feePlanService = feePlanService;
	}
	@Autowired
	public void setFeeScaleService(FeeScaleService feeScaleService) {
		this.feeScaleService = feeScaleService;
	}
	@Autowired
	public void setRoomService(RoomService roomService) {
		this.roomService = roomService;
	}
	
}
